package sseGameApps.controller.mdkbr.main;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slim3.controller.Navigation;

import sseGameApps.controller.JsonController;
import sseGameApps.model.mdkbr.MdDecoration;
import sseGameApps.model.mdkbr.MdKbrUser;
import sseGameApps.model.mdkbr.MdUseKanji;
import sseGameApps.service.mdkbr.MdDecorationService;
import sseGameApps.service.mdkbr.MdKanjiService;
import sseGameApps.service.mdkbr.MdKbrUserService;

public class MdEnemySelectController extends JsonController {

    private MdKbrUserService serviceMdKbrUser = new MdKbrUserService();
    private MdKanjiService serviceMdKanji = new MdKanjiService();
    private MdDecorationService serviceMdDecoration = new MdDecorationService();

    @Override
    public Navigation run() throws Exception {

        Integer starCnt = 0;
        Map<String, String> data = new HashMap<String, String>();

        data.put("idCanvas", asString("idCanvas"));
        data.put("userId", asString("userId"));

        MdKbrUser mdKbrUserP =
            serviceMdKbrUser.getMdKbrUser(asString("userId"));

        List<MdKbrUser> mdKbrUserList =
            serviceMdKbrUser.getEnemyList(mdKbrUserP.getUserLevel());

        Integer cnt = 1;

        if (mdKbrUserList == null) {
            data.put("enemyMntCG1", "m_000R.png");
            data.put("enemyMntId1", "教官");
            data.put("enemyMntNm1", "教官");
            data.put("enemyMntScNm1", mdKbrUserP.getPlayerSchool());
            data.put("enemyMntLv1", mdKbrUserP.getUserLevel().toString());
            data.put("enemyMntStr1", "");
            data.put("enemyMntCG2", "");
            data.put("enemyMntId2", "");
            data.put("enemyMntNm2", "");
            data.put("enemyMntScNm2", "");
            data.put("enemyMntLv2", "");
            data.put("enemyMntStr2", "");
            data.put("enemyMntCG3", "");
            data.put("enemyMntId3", "");
            data.put("enemyMntNm3", "");
            data.put("enemyMntScNm3", "");
            data.put("enemyMntLv3", "");
            data.put("enemyMntStr3", "");
            data.put("enemyMntCG4", "");
            data.put("enemyMntId4", "");
            data.put("enemyMntNm4", "");
            data.put("enemyMntScNm4", "");
            data.put("enemyMntLv4", "");
            data.put("enemyMntStr4", "");
        } else {
            for (MdKbrUser mdKbrUser : mdKbrUserList) {
                if (mdKbrUser.getPlayerName() != null
                    && !"".equals(mdKbrUser.getPlayerName())) {

                    if (!mdKbrUser.getUserId().equals(mdKbrUserP.getUserId())) {

                        MdUseKanji mdUseKanji =
                            serviceMdKanji
                                .getMdUseKanji(mdKbrUser.getUserId());

                        if (mdUseKanji == null) {
                            continue;
                        } else {
                            if (mdUseKanji.getUsingKanji01() == null
                                || mdUseKanji.getUsingKanji02() == null
                                || mdUseKanji.getUsingKanji03() == null
                                || "".equals(mdUseKanji.getUsingKanji01())
                                || "".equals(mdUseKanji.getUsingKanji02())
                                || "".equals(mdUseKanji.getUsingKanji03())) {
                                continue;
                            }
                        }

                        data.put(
                            "enemyMntCG" + cnt.toString(),
                            mdKbrUser.getPlayerCGType());
                        data.put(
                            "enemyMntId" + cnt.toString(),
                            mdKbrUser.getUserId());
                        data.put(
                            "enemyMntNm" + cnt.toString(),
                            mdKbrUser.getPlayerName());
                        data.put(
                            "enemyMntScNm" + cnt.toString(),
                            mdKbrUser.getPlayerSchool());
                        data.put("enemyMntLv" + cnt.toString(), mdKbrUser
                            .getUserLevel()
                            .toString());

                        MdDecoration mdDecoration = serviceMdDecoration.getMdDecoration(mdKbrUser.getUserId());
                        if (mdDecoration != null) {
                            if(mdDecoration.isEventComplete1()){
                                starCnt++;
                            }
                            if(mdDecoration.isEventComplete2()){
                                starCnt++;
                            }
                            if(mdDecoration.isEventComplete3()){
                                starCnt++;
                            }
                        }
                        data.put("enemyMntStr" + cnt.toString(),starCnt.toString());

                        cnt++;
                        if (cnt > 4){
                            break;
                        }
                    }
                }
            }
            if (cnt < 5) {
                data.put("enemyMntCG" + cnt.toString(), "m_000R.png");
                data.put("enemyMntId" + cnt.toString(), "教官");
                data.put("enemyMntNm" + cnt.toString(), "教官");
                data.put(
                    "enemyMntScNm" + cnt.toString(),
                    mdKbrUserP.getPlayerSchool());
                data.put("enemyMntLv" + cnt.toString(), mdKbrUserP
                    .getUserLevel()
                    .toString());
                data.put("enemyMntStr" + cnt.toString(),"0");
            }
        }

        this.toJson(data);

        return null;
    }
}
